package edu.xjdx.os.service.Impl;

import edu.xjdx.os.domain.Customer;
import edu.xjdx.os.domain.CustomerExample;
import edu.xjdx.os.mapper.CustomerMapper;
import edu.xjdx.os.service.CustomerService;
import edu.xjdx.os.utils.IDGenerator;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerMapper mapper;

    @Autowired
    JavaMailSender mailSender;

    @Override
    public Customer findByName(String customerName) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerNameEqualTo(customerName);
        List<Customer> customers = mapper.selectByExample(example);
        Customer customer = null;
        if(customers.size() > 0) {
            customer = customers.get(0);
        }
        return customer;
    }


    @Override
    public Integer login(String customerName, String customerPassword, HttpSession session) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerNameEqualTo(customerName);
        Integer re = 0;
        List<Customer> customers = mapper.selectByExample(example);
        Customer customer = null;
        if(customers.size() > 0) {
            customer = customers.get(0);
        }
        if(customer!=null){
            if(customerPassword.equals(customer.getCustomerPassword())){
                session.setAttribute("customerId",customer.getCustomerId());
                session.setAttribute("customerName",customer.getCustomerName());
                session.setAttribute("customerEmail",customer.getCustomerEmail());
                session.setAttribute("customerPhone",customer.getCustomerPhone());
                session.setAttribute("customerAddress",customer.getCustomerAddress());
                session.setAttribute("customerPassword",customer.getCustomerPassword());
                re = 0;
            }else {
                re = 1;
            }
        }else{
            re = 2;
        }
        return re;
    }
    @Override
    public Integer register(String customerName,String customerEmail,String customerPassword,String customerPhone,String customerAddress){
        Integer re = 0;
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerNameEqualTo(customerName);
        List<Customer> customers = mapper.selectByExample(example);
        Customer customer = null;
        if(customers.size()==0){
            customer = new Customer();
            if (customerName!=null && customerEmail!=null && customerPassword!=null && customerPhone!=null && customerAddress!=null) {
                customer.setCustomerId(IDGenerator.getId());
                customer.setCustomerName(customerName);
                customer.setCustomerEmail(customerEmail);
                customer.setCustomerPhone(customerPhone);
                customer.setCustomerAddress(customerAddress);
                customer.setCustomerPassword(customerPassword);
                try {
                    mapper.insert(customer);
                    re = 0;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                re = 2;
            }
        }else {
            re = 1;
        }
        return re;
    }

    @Override
    public int addCustomer(Customer customer) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerNameEqualTo(customer.getCustomerName());
        List<Customer> customers = mapper.selectByExample(example);
        if(customers.size()==0){
            customer.setCustomerId(IDGenerator.getId());
            mapper.insert(customer);
            return 0;
        }
        else {
            return 1;
        }
    }

    @Override
    public List<Customer> listAll() {
        return mapper.findAll();
    }

    @Override
    public boolean remove(String id) {
        try {
            mapper.deleteByPrimaryKey(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean update(Customer customer) {
        try {
            if(customer.getCustomerId()!=null){
                mapper.updateByPrimaryKeySelective(customer);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Integer change(Customer customer, String oldPassword, String newPassword,String confirm) {
        Integer re = 0;
        if(customer.getCustomerPassword().equals(oldPassword)){
            if(newPassword!=null && confirm!=null && newPassword.equals(confirm)){
                customer.setCustomerPassword(newPassword);
                try {
                    mapper.updateByPrimaryKeySelective(customer);
                    re = 0;
                } catch (Exception e) {
                    e.printStackTrace();
                    //数据库修改出问题
                    re = 3;
                }
            }else {
                //两次密码不一样
                re = 2;
            }
        }else{
            //原密码不正确
            re = 1;
        }
        return re;
    }

    @Override
    public boolean modify(String customerEmail, String customerPhone, String customerAddress, Session session) {
        return false;
    }

    @Override
    public Integer findEmail(String customerName, String customerEmail,HttpSession session) {
        Integer re = 0;
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerNameEqualTo(customerName);
        List<Customer> customers = mapper.selectByExample(example);
        String mail = RandomStringUtils.randomAscii(4);
        Customer customer = null;
        if(customers.size() > 0) {
            customer = customers.get(0);
            if(customer.getCustomerEmail().equals(customerEmail)){
                try {
                    //发送邮件
                    MimeMessage mimeMessage = this.mailSender.createMimeMessage();
                    MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                    message.setFrom("865721755@qq.com");
                    message.setTo(customer.getCustomerEmail());
                    message.setSubject("验证码");
                    message.setText(mail);
                    this.mailSender.send(mimeMessage);
                    session.setAttribute("mail",mail);
                    session.setAttribute("cus",customer);

                    //用户存在，邮箱正确，邮件发送成功
                    re = 0;
                } catch (Exception ex) {
                    ex.printStackTrace();
                    //邮件发送失败
                    re = 3;
                }

            }else {
                //邮箱错误
                re = 2;
            }
        }else {
            //用户不存在
            re = 1;
        }
        return re;
    }

    @Override
    public Integer check(String verification, HttpSession session) {
        Integer re = 0;
        if(verification.equals((String)session.getAttribute("mail"))){
            re = 0;
        }else {
            re = 1;
        }
        return re;
    }

    @Override
    public Integer forget(String newPassword, String confirm,Customer customer) {
        Integer re = 0;
        if(newPassword.equals(confirm)){
            customer.setCustomerPassword(newPassword);
            try {
                mapper.updateByPrimaryKeySelective(customer);
                re = 0;
            } catch (Exception e) {
                e.printStackTrace();
                //数据库修改出问题
                re = 2;
            }
        }else {
            re = 1;
        }
        return re;
    }

    @Override
    public Customer findById(String id) {
        return mapper.selectByPrimaryKey(id);
    }

    //    @Override
//    public Customer findById(String customerId) {
//        Customer customer=new Customer();
//        customer=mapper.selectByPrimaryKey(customerId);
//        return customer;
//    }
    //    @Override
//    public Integer update(String name, String email, String pwd, Customer user){
//        if(user!=null){
//            if(name!=null && email!=null && pwd!= null){
//                user.setUpwd(pwd);
//                user.setUname(name);
//                user.setEmail(email);
//
//                try {
//                    userMapper.updateUser(user);
//                    return 1;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
 //       return 0;
//    }
}
